﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GpuProgramCompiler
{
    class CmdLineArgs
    {
        public class SupportedArg
        {
            public enum RequirementType
            {
                Required,
                Optional,
            };

            public SupportedArg(String inName, String inDesc, RequirementType inReqType)
            {
                name = inName;
                desciption = inDesc;
                requirement = inReqType;
            }

            private String name;
            private String desciption;
            private RequirementType requirement;

            public String Name
            {
                get { return name; }
                set { name = value; }
            }

            public String Description
            {
                get { return desciption; }
                set { desciption = value; }
            }

            public RequirementType Requirement
            {
                get { return requirement; }
                set { requirement = value; }
            }
        }

        private Dictionary<String, String> args = new Dictionary<String, String>();
        private List<SupportedArg> supportedArgs = new List<SupportedArg>();

        public List<SupportedArg> SupportedArgs
        {
            get { return supportedArgs; }
        }

        public bool Initialise(String[] inArgs)
        {
            foreach (String inArg in inArgs)
            {
                if (inArg.StartsWith("/"))
                {
					String theArg = inArg.Substring(1);
					int colonPos = theArg.IndexOf(':');
					if (colonPos != -1)
					{
						String theSwitch = theArg.Substring(0, colonPos);
						String theValue = theArg.Substring(theArg.IndexOf(':') + 1);
						args[theSwitch] = theValue;
					}
					else
					{
						args[theArg] = "";
					}
                }
                else
                {
                    args["IN"] = inArg;
                }
            }

            // validate args
            foreach (SupportedArg supportedArg in supportedArgs)
            {
                if (supportedArg.Requirement == SupportedArg.RequirementType.Required)
                {
                    if (!args.ContainsKey(supportedArg.Name))
                    {
                        return false;
                    }
                }
            }

            return true;
        }

        public String this[String key]
        {
            get 
			{
				if (args.ContainsKey(key))
					return args[key];
				else 
					return null;
			}
            set { args[key] = value; }
        }

        public void PrintUsage()
        {
            int longestName = 0;
            foreach (SupportedArg arg in supportedArgs)
            {
                longestName = Math.Max(longestName, arg.Name.Length);
            }
            int nameLengh = longestName + 1;

            foreach (SupportedArg arg in supportedArgs)
            {
                String line = arg.Name;
                for (int i = line.Length; i < nameLengh; ++i)
                    line += " ";
                System.Console.WriteLine(String.Format("/{0}- {1}", line, arg.Description));
            }
        }
    }
}
